<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //map
        var obj = { 101: { name: "tom", id: 101 }, 102: { name: "jack", id: 102 } };
        console.log(101 in obj);

        //根据用户编号查找用户
        var searchUser = function () {
            var cached = {};  //用于缓存数据的对象
            return function (id) {
                if (!(id in cached))  //缓存中如果没有该对象
                {
                    cached[id] = { name: Math.random(), id: id };  //模拟添加
                }
                return cached[id];
            };
        }();

        console.log(searchUser(101));
        console.log(searchUser(102));
        console.log(searchUser(103));
        console.log(searchUser(101));
        console.log(searchUser(102));
        console.log(searchUser(103));
        console.log(searchUser(101));
        console.log(searchUser(102));
        console.log(searchUser(103));
    </script>
</body>

</html>